package tt.dz.egobus.servicemanager.core.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.util.WebUtils;
import tt.dz.egobus.servicemanager.bean.UserSession;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Anson
 * @date 17-4-20
 * @name session 超时处理
 * @since 1.0.4
 */
public abstract class SessionInterceptor extends HandlerInterceptorAdapter {
    private static final Logger LOGGER = LoggerFactory.getLogger(SessionInterceptor.class);

    public static final String ADMIN_SESSION_KEY = "EGOBUS_ADMIN_SESSION";

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {

        if (!this.isFilter(request)) {
            return true;
        }
        UserSession userSession = this.getUserSession(request);
        if (this.validateSession(userSession)) {
            return this.successSessionValidate(userSession, request,
                    response, handler);
        } else {
            return this.invalidateSessionValidate(userSession, request,
                    response, handler);
        }
    }


    /**
     * @param request
     * @author Anson
     * @date 17-4-20
     * @name 获取session值
     * @since 1.0.4
     */
    protected UserSession getUserSession(HttpServletRequest request) {
        Object adminSession = WebUtils.getSessionAttribute(request,
                ADMIN_SESSION_KEY);
        if (adminSession == null) {
            return null;
        }
        UserSession userSession = UserSession.jsonToBean(adminSession.toString());
        return userSession;
    }


    /**
     * @param userSession 用户session
     * @author Anson
     * @date 17-4-20
     * @name 判断当前是否有效的session，处理空session,过期session
     * false : session失效 ， true ： session有效
     * @since 1.0.4
     */
    protected boolean validateSession(UserSession userSession) {
        if (userSession == null) {
            LOGGER.info("当前用户未登录");
            return false;
        }
        if (userSession.isExpire()) {
            LOGGER.info("当前用户session超时 user={}", userSession);
            return false;
        }
        return true;
    }

    protected abstract boolean isFilter(HttpServletRequest request);

    /**
     * @param userSession 当前session
     * @param request
     * @param response
     * @param handler
     * @author Anson
     * @date 17-4-20
     * @name 若session验证成功的后置处理
     * @since 1.0.4
     */
    protected abstract boolean successSessionValidate(UserSession userSession, HttpServletRequest request,
                                                      HttpServletResponse response, Object handler);

    /**
     * @param userSession 当前session
     * @param request
     * @param response
     * @param handler
     * @author Anson
     * @date 17-4-20
     * @name 若session失败的后置处理
     * @since 1.0.4
     */
    protected abstract boolean invalidateSessionValidate(UserSession userSession, HttpServletRequest request,
                                                         HttpServletResponse response, Object handler);
}
